package cn.tedu.tmall.front.content.persist.respository.impl;

import cn.tedu.tmall.common.pojo.PageData;
import cn.tedu.tmall.front.content.persist.mapper.ArticleDetailMapper;
import cn.tedu.tmall.front.content.persist.mapper.ArticleMapper;
import cn.tedu.tmall.front.content.persist.respository.IArticleRepository;
import cn.tedu.tmall.front.content.pojo.po.ArticleDetailPO;
import cn.tedu.tmall.front.content.pojo.po.ArticlePO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class ArticleRepositoryImpl implements IArticleRepository {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleDetailMapper articleDetailMapper;

    @Override
    public PageData<ArticlePO> getArticleByCategoryId(Long categoryId, Integer pageNum, Integer pageSize) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("category_id",categoryId);
        log.debug("通过类别id查询文章列表:QueryWrapper:{}",wrapper);
        //使用pageHelper分页开始
        PageHelper.startPage(pageNum,pageSize);
        List<ArticlePO> list = articleMapper.selectList(wrapper);
        //list 返回的page, 我们方法定义的是返回的是 List<ArticlePO> 这个家伙,为什么没有报错
        //数据封装把pageHelper的pageInfo 转换为我们系统通用的pageData对象
        PageInfo<ArticlePO> articlePOPageInfo = new PageInfo<>(list);
        PageData<ArticlePO> pageData = new PageData<>();
        BeanUtils.copyProperties(articlePOPageInfo,pageData);
        return pageData;
    }

    @Override
    public ArticlePO getArticleById(Long articleId) {
        ArticlePO articlePO = articleMapper.selectById(articleId);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("article_id",articleId);
        ArticleDetailPO articleDetailPO = articleDetailMapper.selectOne(wrapper);
        //todo detail 没有处理
        return articlePO;
    }
}
